<template>
  <div class="header">
    <div class="page-title">轮播图管理</div>
    <el-button
      class="m-t-15"
      type="primary"
      size="small"
      icon="el-icon-edit"
      @click="handleCCreate"
      >添加图片</el-button
    >
    <el-table :data="tableData" class="m-t-5">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="id" width="100"></el-table-column>
      <el-table-column prop="name" label="轮播图名称" ></el-table-column>
      <el-table-column label="操作" min-width="200">
        <template #default="v">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            class="blue-btn"
            @click="handleEdit(v.row)"
            >编辑
          </el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="handleDelete(v.row)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      hide-on-single-page
      layout="prev, pager, next"
      :total="total"
      :current-page="current"
      @current-change="changePage"
    >
    </el-pagination>
  </div>

  <el-dialog title="轮播图操作" v-model="dialogFormVisible" width="35%">
    <el-form>
      <el-form-item label="名称">
        <el-input
          v-model="form.name"
          autocomplete="off"
          placeholder="请输入名称"
        ></el-input>
      </el-form-item>
    
      <el-form-item label="轮播图">
        <el-upload
          class="avatar-uploader"
          :action="uploadUrl"
          ref="upload"
          :show-file-list="false"
          :on-success="handleUploadSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="form.image_path" :src="form.image_path"  style="width:100px" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
     </el-form-item>
      <el-form-item label="排序">
        <el-input
          v-model="form.sort"
          autocomplete="off"
          placeholder="请输入排序"
        ></el-input>
      </el-form-item>
    </el-form>

    <template v-slot:footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmitForm">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>

import { UPLOAD_URL,CONST_DMOMAIN_URL } from "@/config/env";
import { request_banner_list, request_banner_create, request_banner_update, request_banner_delete } from "@/util/api";

export default {
  name: "SeekType",
  data() {
    return {
      current: 1,
      total: 0,
      form: {},
      uploadUrl: UPLOAD_URL,
      dialogFormVisible: false,
      actionType: 0, // 1 添加  2 编辑
      header: { token: 1 },
    };
  },
  computed: {},
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.$nextTick(() => {
        request_banner_list()
          .then((res) => {
            const result = res.data;
            if (result.code == 200) {
              this.tableData = result.data.data;
              this.total = result.data.total;
              this.current = result.data.current;
            }
          })
          .catch();
      });
    },
    
     //发布图片上传成功的回调函数
    handleUploadSuccess(res) {
      this.form.image_path = CONST_DMOMAIN_URL + res.data;
    },

    //图片上传前的验证函数
    beforeAvatarUpload(file) {
      let types = ['image/jpeg', 'image/jpg', 'image/gif', 'image/bmp', 'image/png'];
      const isJPG=types.includes(file.type)
      // const isJPG = file.type === "image/png";验证单张图片
      const isLt2M = file.size / 1024 / 1024 < 3;
      if (!isJPG) {
        this.$message.error("上传图片只能是 jpg,jpeg,bmp,png 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 3MB!");
      }
      return isJPG && isLt2M;
    },

    // 添加
    handleCCreate() {
      this.actionType = 1;
      this.form = {};
      this.dialogFormVisible = true;
    },
    // 编辑
    handleEdit(row) {
      this.actionType = 2;
      this.form = row;
      this.dialogFormVisible = true;
    },
    // 提交
    handleSubmitForm() {
      let apiFunc;
      switch (this.actionType) {
        case 1: // 添加
            apiFunc = request_banner_create; 
          break;
        case 2: // 编辑
            apiFunc = request_banner_update;
          break;
        default:
          this.$message.error({
            message: "操作类型不正确",
            offset: 70,
          });
          return false;
      }
      apiFunc({
        ...this.form,
      }).then((res) => {
        const result = res.data;
        if (result.code == 200) {
          this.dialogFormVisible = false;
          this.$notify.success({
            title: "系统提示",
            message: result.message,
            offset: 70,
          });
          this.changePage(1);
        } else {
          this.$message.error({
            message: result.message,
            offset: 70,
          });
        }
      });
    },
    // 删除
    handleDelete(row) {
      this.$confirm("此操作将永久删除，是否继续？", "系统提示", {
        type: "warning",
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(() => {
          request_banner_delete({
            id: row.id,
          })
            .then((res) => {
              const result = res.data;
              if (result.code == 200) {
                this.$message.success({
                  message: "删除成功！",
                  offset: 70,
                });
                this.changePage(this.page)
              } else {
                this.$message.error({
                  message: result.message,
                  offset: 70,
                });
              }
            })
            .catch();
        })
        .catch(() => {
          this.$message.info({
            message: "已取消删除！",
            offset: 70,
          });
        });
    },
    // 分页
    changePage(page) {
      request_banner_list({
        page,
      })
        .then((res) => {
          const result = res.data;
          if (result.code == 200) {
            this.tableData = result.data.data;
            this.total = result.data.total;
            this.current = result.data.current;
          }
        })
        .catch();
    },
  },
};
</script>

<style scoped  lang="less">
</style>